var width;
var height;
var Apolyline = require('../../utils/lalo.js')
const innerAudioContext = wx.createInnerAudioContext()
//获取用户手机长度与宽度信息，自定适应不同手机
wx.getSystemInfo({
  success: function (res) {
    width = res.windowWidth;
    height = res.windowHeight;
  }
});
var controlRight = parseInt(width) * 0.1;
var app = getApp();
Page({
  data: {
    //返回当前轨迹的id
    id: '',
    //用于判断当前行走轨迹是否有一次在预定路线
    count: '',
    //表示当前是第几次记录轨迹,默认为1
    count1: 1,
    //如果是第二次记录轨迹先判断是否清空路线
    count2: 1,
    flag: true, 
    phone: '',
    scale: 15,
    longitude: '',
    latitude: '',
    width: width,
    height: height,
    polyline: Apolyline.polyline,
    markers: [{
      iconPath: '../../image/0.gif',
      title: '南昌站',
      id: 0,
      latitude: 28.662991163527906,
      longitude: 115.91763209358216,
      width: 25,
      height: 25,

    },
    {
      iconPath: '../../image/1.gif',
      title: '检测大厦',
      id: 1,
      latitude: 28.66371,
      longitude: 115.92804,
      width: 25,
      height: 25
    },
    {
      iconPath: '../../image/2.gif',
      title: '青山湖景区',
      id: 2,
      latitude: 28.693688345437906,
      longitude: 115.92312525764466,
      width: 35,
      height: 25
    },
    {
      iconPath: '../../image/6.gif',
      title: '英雄大桥',
      id: 3,
      latitude: 28.714432490959112,
      longitude: 115.9147747094345,
      width: 25,
      height: 30
    },

    {
      iconPath: '../../image/8.gif',
      title: '孔目湖',

      id: 4,
      latitude: 28.738840131557982,
      longitude: 115.88743227073668,
      width: 30,
      height: 25
    },

    {
      iconPath: "../../image/jxlg1.gif",
      title: "江西理工大学",

      id: 5,
      latitude: 28.74154104464624,
      longitude: 115.876900067482,
      width: 25,
      height: 25
    }
    ],
    controls: [
      {
        id: 1,
        iconPath: '../../image/search.png',
        position: {
          width: width,
          height: 50,
          top: 20,
          right: controlRight
        },
        clickable: true
      },
      {
        id: 2,
        iconPath: '../../image/jia.png',
        position: {
          width: 30,
          height: 30,
          top: 200 - 50,
          left: 280
        },
        clickable: true
      },
      {
        id: 3,
        iconPath: '../../image/jian.png',
        position: {
          width: 30,
          height: 30,
          top: 230 - 50,
          left: 280
        },
        clickable: true
      },
      {
        id: 4,
        iconPath: '../../image/location-control.png',
        position: {
          width: 30,
          height: 30,
          top: 280,
          left: 280
        },
        clickable: true
      },
      {
        id: 5,
        iconPath: '../../image/route.png',
        position: {
          width: 40,
          height: 40,
          top: 350,
          left: 275
        },
        clickable: true
      }, {

        id: 6,
        iconPath: '../../image/circle2.png',
        position: {
          width: 35,
          height: 35,
          top: 350,
          left: 30
        },
        clickable: true
      },{
        id: 7,
        iconPath: '../../image/remind.png',
        position: {
          width: 30,
          height: 30,
          top: 400,
          left: 30
        },
        clickable: true
      }
    ]
  },
  onShow: function () {},
  
  onReady: function () {
    var that = this
    var status = app.appData.systemUser.status;

    var polyline = this.data.polyline
    that.setTimeoutEvent3 = setInterval(function () {
      wx.getLocation({
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          if (Math.abs(28.7374516490 - latitude)
            < 0.0009 &&
            Math.abs(115.8885043859 - longitude) < 0.0009) {
            innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
            innerAudioContext.play()
            console.info('到达孔目湖景区')
          }
          else if (Math.abs(28.7129848743 - latitude)
            < 0.0009 &&
            Math.abs(115.9161150455 - longitude) < 0.0009) {
            innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
            innerAudioContext.play()
            console.info('到达英雄大桥景区')
          }
          else if (Math.abs(28.6936369694 - latitude)
            < 0.0009 &&
            Math.abs(115.9231638908 - longitude) < 0.0009) {
            innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
            innerAudioContext.play()
            console.info('到达青山湖景区')
          }
        },

      })



    }, 5000)

    if(status=='未登录'){
      wx.redirectTo({
        url: '../login/login',
        success: function(){
          wx.hideTabBar({
            aniamtion:true,
            success:function(){
        }
          });
        },
        fail:function(e){
         console.log('eeeeeeeeeeeeeeee'+e)
        }
      })
    }
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      }
    });
     
  },
  controltap(e) {
    var that = this;
    //地图放大控件
    if (e.controlId == 2) {
      that.setData({
        scale: ++that.data.scale
      });
    }
    //地图缩小控件
    else if (e.controlId == 3) {
      that.setData({
        scale: --that.data.scale
      });
    }
    //进入搜索地点
    else if (e.controlId == 1) {
      wx.chooseLocation({
        success: function (res) {
          console.log(res);
          wx.openLocation({
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name
          })
          //刷新页面数据


        },
        fail: function () {
          console.log('选择位置出错!');
        }
      })
    }
    //点击中心空间，获得当前的位置
    else if (e.controlId == 4) {
      var that = this;
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          })
        },
      })
    }
    //点击路线空间，跳转到搜索页面
    else if (e.controlId == 5) {
      wx.navigateTo({
        url: '../../pages/detail/detail',
        success: function () {
          console.log('跳转成功')
        },
        fail: function () {
          console.log('跳转失败');
        }
      })
    }
    //行走记录
    else if (e.controlId == 6) {
      var polyline = this.data.polyline
      var points = polyline[1].points
      if (this.data.controls[5].iconPath == '../../image/circle1.png') {      
        //每次重新记录轨迹都先把原来路线清空    
        polyline[1].points = []
        clearInterval(this.setTimeoutEvent1);
        this.data.controls[5].iconPath = '../../image/circle2.png';
      }   
      else {
        //测试用
        var i=1;
        var id = this.data.id;
        var latlon = new Object();
        var count1 = this.data.count1
        //var userId = app.appData.systemUser.id;
        var phone = app.appData.systemUser.phone;
        this.data.controls[5].iconPath = '../../image/circle1.png';

        //定时器，每5秒获取一次当前位置
        that.setTimeoutEvent1 = setInterval(function () { 
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              polyline[1].points.push({
                latitude: res.latitude,
                longitude: res.longitude
              });       
              that.setData({
                 polyline: polyline ,
              });
            }
          });

          var points = polyline[1].points;

          //第一次请求，创建一个新的latlon
          if (count1 == 1) {
            wx.request({
              url: app.appData.contextPath + 'latlonServlet',
              data: {
                latlon: JSON.stringify(points),
                //userId: userId,
                phone: phone
              },
              success: function (res) {
                id = res.data
                console.log("返回的" + res.data)
              },
              header: {
                'content-type': 'application/json' // 默认值
              }
            })
            count1 = 2;
          }

          //第二次及其以后，则更改其中latlon
          else {
            wx.request({
              url: app.appData.contextPath + 'pointsServlet',
              data: {
                latlon: JSON.stringify(points),
                id: id,
                //userId: app.appData.systemUser.id,
                phone: phone
              },
              success:function(res)
              {
                  console.log(points.length+'pointslength');
              },
              header: {
                'content-type': 'application/json' // 默认值
              }
            })
          }
        }, 1000);

       
          
      }

      //更新数据
      this.setData({
        controls: this.data.controls,
        polyline: polyline
      });

    }
    //判断用户是否在正确路线上，如不则会语音提示
    else if (e.controlId == 7){
      const innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = true
      if (this.data.controls[6].iconPath == '../../image/ringremind.png') {
        this.data.controls[6].iconPath = '../../image/remind.png';
        innerAudioContext.stop();
        clearInterval(this.setTimeoutEvent2);
      } 
      else {
        innerAudioContext.src = 'http://mp.111ttt.cn/mp3music/59457072.mp3'
        innerAudioContext.play()
        console.info('开始监听行程')
        
        this.data.controls[6].iconPath = '../../image/ringremind.png'; 
        var points = this.data.polyline[0].points
        var flag = this.data.flag
        var count = this.data.count
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        //每五秒计时一次
        that.setTimeoutEvent2 = setInterval(function () {
          wx.getLocation({
            type: 'gcj02',
            success: function(res) {
              latitude = res.latitude
              longitude = res.longitude
            },
          })
          count = 0
          for (var i = 0; i < points.length; i++) {
            if (Math.abs(points[i].latitude - latitude) 
            < 0.0003 &&
              Math.abs(points[i].longitude - longitude) < 0.0003) {
              count = 1
              break;
              
            }
          }
 
          if(count == 1){
            flag = true
          }
          else{
            flag = false
          }
          if(!flag){           
            innerAudioContext.src = 'http://mp.111ttt.cn/mp3music/59457072.mp3'
            innerAudioContext.play()
            console.info('位置错误')
          }
          else{
            innerAudioContext.stop()
            if (Math.abs(points[457].latitude - latitude)
              < 0.0003 &&
              Math.abs(points[457].longitude - longitude) < 0.0003) {
              innerAudioContext.src = 'https://y.qq.com/portal/player.html'
              innerAudioContext.play()
              console.info('到达目的地')
            }
          }
        }, 5000);
      }
      this.setData({
        controls: this.data.controls
      });
    }
  },

  markertap: function (e) {
    // 可以改变标记点的图片，也可以显示 一段 描述
    wx.showToast({
      title: "3"
    });
    console.log(e);
  },
  markertap(e) {
    var that = this;
    if (e.markerId == 0) {
      wx.navigateTo({
        url: '../../pages/ncz/ncz',
      })
    }
    else if (e.markerId == 1) {
      wx.navigateTo({
        url: '../../pages/jc/jc',
      })
    }
    else if (e.markerId == 2) {
      wx.navigateTo({
        url: '../../pages/qsh/qsh',
      })
    }
    else if (e.markerId == 3) {
      wx.navigateTo({
        url: '../../pages/yxdq/yxdq',
      })
    }
    else if (e.markerId == 4) {
      wx.navigateTo({
        url: '../../pages/kmh/kmh',
      })
    }
    else if (e.markerId == 5) {
      wx.navigateTo({
        url: '../../pages/jxlg/jxlg',
      })
    }
  },
  aaa:function(){
    var that = this
    var polyline = this.data.polyline
    var points = polyline[1].points
    if (this.data.controls[5].iconPath == '../../image/circle1.png') {
      //每次重新记录轨迹都先把原来路线清空    
      polyline[1].points = []
      clearInterval(this.setTimeoutEvent1);
      this.data.controls[5].iconPath = '../../image/circle2.png';
    }
    else {
      var id = this.data.id;
      var latlon = new Object();
      var count1 = this.data.count1
      //var userId = app.appData.systemUser.id;
      var phone = app.appData.systemUser.phone;
      this.data.controls[5].iconPath = '../../image/circle1.png';

      //定时器，每5秒获取一次当前位置
      that.setTimeoutEvent1 = setInterval(function () {
        wx.getLocation({
          type: 'gcj02', //返回可以用于wx.openLocation的经纬度
          success: function (res) {
            polyline[1].points.push({
              latitude: res.latitude,
              longitude: res.longitude
            });
            that.setData({
              polyline: polyline,
            });
          }
        });

        var points = polyline[1].points;

        //第一次请求，创建一个新的latlon
        if (count1 == 1) {
          wx.request({
            url: app.appData.contextPath + 'latlonServlet',
            data: {
              latlon: JSON.stringify(points),
              //userId: userId,
              phone: phone
            },
            success: function (res) {
              id = res.data
              console.log("返回的" + res.data)
            },
          })
          count1 = 2;
        }

        //第二次及其以后，则更改其中latlon
        else {
          console.log("发出的" + id)
          wx.request({
            url: app.appData.contextPath + 'pointsServlet',
            data: {
              latlon: JSON.stringify(points),
              id: id,
              //userId: app.appData.systemUser.id,
              phone: phone
            },
            success: function (res) {
              console.log('1');
            },
            header: {
              'content-type': 'application/json' // 默认值
            }
          })
        }
      }, 5000);
    }
    //更新数据
    this.setData({
      controls: this.data.controls,
      polyline: polyline
    });   
  }
})
